/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#include "ak_task.h"
#include "ak_lock.h"

/* Default Trace Level */
static 	int 	_gTraceLvl_Task = TRACELVL_WARNING;

/* ---------------------------------------------------------------------------
** OP Functions & Marcos: Task Lib
*/
/* Linux */
#if 	_TARGET_OS == _OS_GNU
#ifdef 	_USE_CAPS_
#include <sys/resource.h>
#include <linux/capability.h>
#include <sched.h>
#endif
static 	void 	STask_Envior(void){
	return;
}
void 	STask_PriSet(_t_sysTask tid, int nPri){
	return;
}

void 	STask_Suspend(_t_sysTask tid){	
	return;
}

void 	STask_Resume(_t_sysTask tid){	
	return;
}

void 	STask_Delete(_t_sysTask tid){
	pthread_cancel(tid);
	return;
}

_t_sysTask 	STask_Spawn(_TASKENTRY 	  		entry,
						void * 				param,
						int 				stackSize, 
						int 				priority)
{
	pthread_t		thdl;
	pthread_attr_t  attr;
	{
	    if( (pthread_attr_init(&attr) == _E_SUCC) &&
			(pthread_attr_setstacksize(&attr, stackSize) == _E_SUCC)){
			pthread_create(&thdl, &attr, entry, param);
	    }
	}
	SYS_TRACE(_gTraceLvl_Task, TRACELVL_DEBUG, "Task Spawn: 0X%lx", thdl);
	return 	thdl;
}


/* Windows */
#elif 	_TARGET_OS == _OS_WIN
#define STASK_RELINQUISH()	SwitchToThread()

static 	_t_sysHandle 	_gSTask_Heap = NULL;
static 	SYSTEM_INFO 	_gSTask_Info;

static 	void 	STask_Envior(void)
{
	GetSystemInfo(&_gSTask_Info);
	_gSTask_Heap = GetProcessHeap();
	return;
}

void 	STask_Suspend(_t_sysTask tid){
	SuspendThread((_t_sysHandle)tid);
}

void 	STask_Resume(_t_sysTask tid){
	ResumeThread((_t_sysHandle)tid);
}

void 	STask_Delete(_t_sysTask tid){
	TerminateThread((_t_sysHandle)tid, 0);
	return;
}

void 	STask_PriSet(_t_sysTask tid, int nPri){
	SetThreadPriority((_t_sysHandle)tid, nPri);
	return;
}

_t_sysTask 		STask_Spawn(_TASKENTRY 	 	entry,
							void * 				param,
							int 				stackSize, 
							int 				priority)
{
	uintptr_t		thdl;
	{
		thdl = _beginthreadex(NULL, stackSize, entry, param, CREATE_SUSPENDED, NULL);
		if(thdl != (uintptr_t)(0)){
			STask_Resume(thdl);
		}
	}
	return thdl;
}
#endif


void 	STask_LibInit(void)
{
	STask_Envior();
	return;
}


